﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_badge : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private StaticProvider sp = new StaticProvider();
	private ImageProvider ip = new ImageProvider();
	private ServerPath serverpath = new ServerPath();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(!IsPostBack) {
			if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
				Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
				BadgeSettings bs = ip.GetBadgeSetting(acc.ID);
				string code = "";

				if(bs != null && bs.ID != "") {
					code = ip.GenerateZapprBadge(
						bs,
						serverpath.Path + @"/Badges/",
						serverpath.Path + @"/Images/"
					);

					if(code != "") {
						plcCode.Visible = true;
						txtCode.Attributes.Add("onfocus", "this.select();");
						txtCode.Text = code;
					}

					if(bs.BadgeVertical)
						ddlArrangement.SelectedIndex = 0;
					else
						ddlArrangement.SelectedIndex = 1;

					txtImageAmount.Text = bs.ImageAmount.ToString();
					txtLinkColor.Text = bs.LinkColor;
					txtImageBorderColor.Text = bs.ImageBorderColor;
					txtImageBorderWidth.Text = bs.ImageBorderWidth.ToString();
					txtBadgeBorderColor.Text = bs.BadgeBorderColor;
					txtBadgeBorderWidth.Text = bs.BadgeBorderWidth.ToString();

					ddlBadgeBorderStyle.SelectedValue = bs.BadgeBorderStyle;
					ddlImageBorderStyle.SelectedValue = bs.ImageBorderStyle;
					ddlZapprLinkPosition.SelectedValue = bs.ZapprLinkPosition.ToString();
				}
			}
			else {
				Response.Redirect("~/login.aspx");
			}
		}
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			BadgeSettings bs = ip.GetBadgeSetting(acc.ID);

			bs.BadgeVertical = (ddlArrangement.SelectedIndex == 0 ? true : false);
			bs.ImageAmount = Convert.ToInt32(txtImageAmount.Text.Trim());
			bs.LinkColor = txtLinkColor.Text.Trim();
			bs.ImageBorderColor = txtImageBorderColor.Text.Trim();
			bs.ImageBorderWidth = Convert.ToInt32(txtImageBorderWidth.Text.Trim());
			bs.ImageBorderStyle = ddlImageBorderStyle.SelectedValue;
			bs.BadgeBorderColor = txtBadgeBorderColor.Text.Trim();
			bs.BadgeBorderWidth = Convert.ToInt32(txtBadgeBorderWidth.Text.Trim());
			bs.BadgeBorderStyle = ddlBadgeBorderStyle.SelectedValue;
			bs.ZapprLinkPosition = Convert.ToInt32(ddlZapprLinkPosition.SelectedValue);
			
			try {
				ip.GenerateZapprBadge(
					bs,
					serverpath.Path + @"/Badges/",
					serverpath.Path + @"/Images/"
				);

				Response.Redirect("~/default.aspx?id=badge");
			}
			catch(Exception ex) {
				LiteralControl litError = new LiteralControl();
				litError.Text = "<div class=\"errorBox\">";
				litError.Text += "<span class=\"txtH3\">" + GetLocalResourceObject("Error").ToString() + "</span>";
				litError.Text += "<br /><br />";
				litError.Text += "<span class=\"txtNormal\">" + ex.Message + "</span>";
				litError.Text += "</div>";
				litError.Text += "<br /><br />";

				//errLogin.Controls.Add(new LiteralControl("<br /><br />"));
				errLogin.Controls.Add(litError);
			}
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Response.Redirect("~/default.aspx?id=badge");
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}
}
